<?php
		// id, pay_id, cust_id, totshipping, tothandling, tax, total, subtotal
		// paymentApproved, authCode, ponumber, status NEW, orderitems 1, created xxx, updated yyy
		// shipfname, shiplname, shipaddr1, shipaddr2, shipcity, shipstate, shipzip, shipphone, shipext,
		// payment_name, fname, lname, addr1, addr2, city, state, zip, phone_1, ext_1, phone_2, ext_2
		// fax, companyname, modxcustid, email
		
	function orders_properties() {
		global $kiwee, $_lang;

		$id = intval($_POST['id']);

		$ordinfo = $kiwee->getOrderInfo($id);
		if ($ordinfo == NULL)
		  die('ERROR: Unable to retrieve order ' . $id);

		$lines = array();
		$lines[] = '<div style="float: left; width: 45%; font-size: 20px; color: white; margin-bottom: 10px">';
		$lines[] = '<table border="0" cellpadding="0" cellspacing="0">';
		$lines[] = '<tr><td align="right">Customer #:</td><td width="10">&nbsp;</td><td>' . $ordinfo['cust_id'] . '</td></tr>';
		$lines[] = '<tr><td align="right">PO #:</td><td width="10">&nbsp;</td><td>' . $ordinfo['ponumber'] . '</td></tr>';
		$lines[] = '<tr><td align="right">Payment:</td><td width="10">&nbsp;</td><td><a title="' . $ordinfo['authCode'] . '" ';
		$lines[] = 'style="text-decoration: none; color: #FFFF66;">' . $ordinfo['paymentApproved'] . '</a></td></tr>';
		$lines[] = '</table>';
		$lines[] = '</div>';
		$lines[] = '<div style="float: right; width: 45%; font-size: 20px; color: white; margin-bottom: 10px">';
		$lines[] = '<table border="0" cellpadding="0" cellspacing="0" align="right">';
		$lines[] = '<tr><td align="right">Order #:</td><td width="10">&nbsp;</td><td>' . $ordinfo['cust_id'] . '</td></tr>';
		$lines[] = '<tr><td align="right">Placed:</td><td width="10">&nbsp;</td><td>' . $ordinfo['created'] . '</td></tr>';
		$lines[] = '<tr><td align="right">Status:</td><td width="10">&nbsp;</td><td>' . $ordinfo['status'] . '</td></tr>';
		
		
		
		
		$lines[] = '</table>';
		$lines[] = '</div><div style="clear: both;" />';

		$lines[] = '<div class="billto" style="border:solid 1px; border-color: white; width: 250px; float: left; margin-bottom: 20px; font-weight: bold; height: 210px;">';
		$lines[] = '<div style="background:#8FC70C; height: 20px; margin-left: 0px; width: 250px;">';
		$lines[] = '<p style="color: black; font-size:14px; padding-left: 10px; float: left; width: 250px;" >Bill To:</p></div>';
		$lines[] = '<div style="background:#FFFFFF; height: 180px; padding-top:10px;">';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['fname'] . ' ' . $ordinfo['lname'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['companyname'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['addr1'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['addr2'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['city'] . ', ' . $ordinfo['state'] . '&nbsp;&nbsp;' . $ordinfo['zip'] . '</p><br />';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['phone_1'] . '&nbsp;&nbsp;&nbsp;ext: ' . $ordinfo['ext_1'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['email'] . '</p>';
		$lines[] = '</div></div>';

		$lines[] = '<div class="shipto" style="border:solid 1px; border-color: white; width: 250px; float: right; margin-bottom: 20px; font-weight: bold; height: 210px;">' . "\n";
		$lines[] = '<div style="background:#8FC70C; height:20px; width:250px;">';
		$lines[] = '<p style="color:black; font-size:14px; float: right; width: 250px; padding-left: 10px;">Ship To:</p></div>';
		$lines[] = '<div style="background:#FFFFFF; height: 180px; padding-top:10px;">' . "\n";
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['shipfname'] . ' ' . $ordinfo['shiplname'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['shipcompanyname'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['shipaddr1'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['shipaddr2'] . '</p>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">'.$ordinfo['shipcity'].', '.$ordinfo['shipstate'].'&nbsp;&nbsp;' .$ordinfo['shipzip'].'<br/><br/><br/><br/>';
		$lines[] = '<p style="color: black; font-size:12px; margin-left: 30px;">' . $ordinfo['shipphone'] . '&nbsp;&nbsp;&nbsp;ext: ' . $ordinfo['sipext'] . '</p>';
		$lines[] = '</div></div><div style="clear:both;" />';

		$lines[] = '<div style="padding: 5px 0px 5px 0px; margin: 0 0 0 0; color: black; width: 560px; height: 25px; background: #8FC70C; text-align: center; font-weight: bold; font-size: 16px;">';
		$lines[] = 'Line Items</div>';
		$lines[] = '<div style="margin: 0 0 0 0;">';

		$sql = 'SELECT * from ' . $kiwee->config['fpx'] . 'order_detail where order_id=' . $id;

		$ds = mysql_query($sql) or die ('ERROR: ' . mysql_error());
		$grd2 = new DataGrid('',$ds, $number_of_results);
		$grd2->noRecordMsg = $_lang['no_records_found'];
		$grd2->cssClass='grid';
		$grd2->columnHeaderClass='gridHeader';
		$grd2->itemClass='gridItem';
		$grd2->altItemClass='gridAltItem';
		//edited by Bright @20080807 
		//$grd2->fields='item_id, description, quantity, price, optionsprice, totalprice';
		$grd2->fields='item_id, options, quantity, price, optionsprice, totalprice';
		$grd2->columns= $_lang['order_edit_id'] . ', ' . $_lang['order_edit_description'] . ', ' . $_lang['order_edit_quantity'] . ', ' . $_lang['order_edit_price'] . ', ' .  $_lang['order_edit_optionsprice'] . ', ' . $_lang['order_total'];
		$grd2->colWidths='50,200,50,50,50,50';
		$grd2->colAligns='left,left,center,right,right,right';
		$grd2->colTypes=',,,currency,currency,currency';
		if($listmode=='1') $grd2->pageSize=0;
		if($_REQUEST['op']=='reset') $grd2->pageNumber = 1;
		$lines[] = $grd2->render();

		echo implode("\n", $lines);
		
		echo '</div><div style="clear: both;" />';
		echo '<div style="width: 210px; float: right; background-color: white; color: black; font-size: 12px; padding-top: 10px;">';
		
		echo '<p style="margin-left: 20px; width: 65px; float: left;">Subtotal:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">$</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">' . number_format($ordinfo['subtotal'],2) . '</p>';
		
		echo '<p style="margin-left: 20px; width: 65px; float: left;">Tax:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">$</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">' . number_format($ordinfo['tax'],2) . '</p>';
		
		echo '<p style="margin-left: 20px; width: 65px; float: left;">Handling:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">$</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">' . number_format($ordinfo['tothandling'],2) . '</p>';
		
		echo '<p style="margin-left: 20px; width: 65px; float: left;">Shipping:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">$</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">' . number_format($ordinfo['totshipping'],2) . '</p>';

		echo '<p style="margin-left: 20px; width: 65px; float: left; ">Total:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">$</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">' . number_format($ordinfo['total'],2) . '</p>';

		//added by Bright @20080807 Can display the currency and currency rate when customer order, and the total amount in cust's price
		echo '<p style="margin-left: 20px; width: 65px; float: left; ">Currency</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">' . $ordinfo['currency'] . '    Rate: </p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">'. $ordinfo['currrate'] .'</p>';
		
		echo '<p style="margin-left: 20px; width: 65px; float: left; ">Total in Customer\'s Currency:</p>';
		echo '<p style="margin-left: 10px: width: 10px; float: left;">'. $kiwee->getCurSym($ordinfo['currency']) .'</p>';
		echo '<p style="width: 35px; float:right; padding-left: 10px; margin-right: 5px; text-align:right;">'. number_format($ordinfo['currrate']*$ordinfo['total']) . '</p>';
		
		
		echo '<br />';
		echo '</div><div style="clear: both;" />';
		echo '<div style="margin-top: 100px;">';
		echo '<button id="returntomain" onclick="hideBox();">Close</button>';
		echo '<label>Change Order Status:</label>';	
		
		//added by Bright @ 20080807 
		echo '<div><select id="uordstatus">
			<option value="NEW">NEW</option>
			<option value="PROCESSING">PROCESSING</option>
			<option value="UPDATED">UPDATED</option>
			<option value="SHIPPED">SHIPPED</option>
			<option value="ERROR">ERROR</option>
			<option value="BACKORDER">BACKORDER</option>
			<option value="CANCELLED">CANCELLED</option>
			</select></div>';	
		echo '<button onclick="uorderstatus_update(' . $id . ');">Update</button>';	
		
		echo '</div>';
	}		

	function orders_gridload() {
		global $kiwee, $_lang;
		
		if ($_POST['sortordersby']==""){
			$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="NEW"';
		}
		
		
		switch($_POST['sortordersby']){
			case 'ALLORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid';// edited by Bright @20080807 should be custid not id 
				
			break;
			
			case 'NEWORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="NEW"';
			break;
			
			case 'CANCELLEDORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="CANCELLED"';
			break;
			
			case 'SHIPPEDORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="SHIPPED"';
			break;
			
			case 'ERRORORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="ERROR"';
			break;
			
			case 'PROCESSORDERS':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="PROCESSING"';
			break;

		 	case 'BACKORDERED':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="BACKORDER"';
			break;
			
		 	case 'UPDATED':
				$sql = 'SELECT A.id, A.created, A.paymentApproved, A.status, A.total, A.orderitems, B.fname, B.lname from ' . $kiwee->config['fpx'] . 'order_header as A INNER JOIN ' . $kiwee->config['fpx'] . 'customers as B where A.cust_id=B.custid and A.status="UPDATED"';
			break;
		}	    
	    
	    $ds = mysql_query($sql) or die ("MYSQL: " . mysql_error());
	    $grd = new DataGrid('',$ds, $number_of_results);
	    $grd->noRecordMsg = $_lang["no_records_found"];
	    $grd->cssClass="grid";
	    $grd->columnHeaderClass="gridHeader";
	    $grd->itemClass="gridItem";
  	    $grd->altItemClass="gridAltItem";
   	    $grd->fields="id, created, fname, lname, paymentApproved, status, total, orderitems";
	    $grd->columns= $_lang["order_id"] . ", " . $_lang["order_created"] . ", " . $_lang["order_fname"] . ", " . $_lang["order_lname"] . ", " .  $_lang["order_paymentApproved"] . ", " . $_lang["order_status"] . ", " . $_lang["order_total"] . ", " . $_lang["order_orderitems"] . ", " . $_lang["order_edit"];
        $grd->colWidths="34,30,100,100,40,50,40,20,34";
        $grd->colAligns="left,center,left,left,left,left,left,center,center";
		$grd->colTypes=",,,,,,currency,,template:<button onclick=\"orders_properties([+id+])\">View</button>";
        //$grd->colTypes=",,,,,,currency,,template:<button id=\"ordermoredetail\" onclick=\"ordermoredetail([+id+])\">View</button>";
        if($listmode=='1') $grd->pageSize=0;
        	if($_REQUEST['op']=='reset') $grd->pageNumber = 1;
        		echo $grd->render();		
	}
	
	//added by Bright @ 20080807
	function orderstatus_update() {
		global $kiwee;
		
		$id = intval($_POST['id']);
		$status = addslashes(trim($_POST['status']));
	
		$qry = 'update ' . $kiwee->config['fpx'] . 'order_header set status="' . $status . '" where id=' . $id;
							
		mysql_query($qry);
		orders_gridload();
	}

	$oktxns[] = 'orders_properties';
	$oktxns[] = 'orders_gridload';
	$oktxns[] = 'orderstatus_update';
?>